/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file  Dpe.h
 * @brief Semidrive. AUTOSAR 4.3.1 MCAL Dpe Driver.
 */
#ifndef DPE_H_
#define DPE_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "Dpe_Ip.h"
#include "Std_Types.h"
#include "Dpe_Cfg.h"
#include "Part.h"

/** \brief  DPE Driver Module ID */
#define DPE_MODULE_ID     (2054U)
/** \brief DPE Driver Instance ID */
#define DPE_INSTANCE   (0x00U)

/**
* @brief Development errors.
*/
#define DPE_E_PARAM_POINTER       ((uint8)0x01U)
#define DPE_E_PARAM_HANDLE        ((uint8)0x02U)
#define DPE_E_PARAM_DATA_LENGTH   ((uint8)0x03U)
#define DPE_E_PARAM_CONTROLLER    ((uint8)0x04U)
#define DPE_E_UNINIT              ((uint8)0x05U)
#define DPE_E_TRANSITION          ((uint8)0x06U)
#define DPE_E_PARAM_BAUDRATE      ((uint8)0x07U)
#define DPE_E_ICOM_CONFIG_INVALID ((uint8)0x08U)
#define DPE_E_INIT_FAILED         ((uint8)0x09U)
#define DPE_E_INVALID_CONTROLLER  ((uint8)0x0AU)
#define DPE_E_TIMEOUT             ((uint8)0x0BU)
#define DPE_E_REINIT              ((uint8)0x0CU)

/**
 * * @brief Service ID (APIs) for Det reporting.
 * */
#define DPE_SID_INIT                          ((uint8)0x00U)
#define DPE_SID_DEINIT                        ((uint8)0x01U)
#define DPE_SID_PFCGETCOUNT                   ((uint8)0x02U)

/** \brief Cache line length are 64 bytes */
#define DPE_CONFIG_ARCH_CACHE_LINE    CACHE_SIZE

/** \brief Type for xtrg callback */
/** Traceability    :SWSR_XTRG_012 */
typedef void (*Dpe_DiagVcanCallbackType)(void *arg, Dpe_VcanStatusType status);

typedef enum
{
    /* dpe uninit */
    DPE_UNINIT = 0U,
    DPE_INIT = 1U,
} Dpe_ControllerStateType;

typedef struct
{
    uint32 index;
    uint32 maxMbNum;
    uint16 mbStartOffset1;
    uint16 mbStartOffset2;
} Dpe_CanInfoType;

typedef struct
{
    uint32 *rtlbIndexAddr;
    uint32 indexSize;
    uint32 *rtlbContextAddr;
    uint32 contextSize;
} Dpe_RouteConfigType;

typedef struct
{
    uint8 qid;
    Dpe_SubqueueArbitSchemeType scheme;
    bool subqueueReplaceEn;
} Dpe_SubQueueTinyConfigType;

typedef struct
{
    uint8 txqueueIndex;
    bool txqueueMbMode;//true: 64byte or false: 8byte
    const Dpe_SubQueueTinyConfigType *subQueueTinyConfig;
    uint8 subqueueNum;
} Dpe_QueueConfigType;

typedef struct
{
    uint8 pfcIndex;
    const Dpe_PfcCfgType *pfcCfg;
} Dpe_PfcTinyConfigType;

typedef struct
{
    Dpe_BusIdType bus;
    uint8 subQid;
} Dpe_MirrorBusConfigType;

typedef struct
{
    const Dpe_PfcTinyConfigType *pfcTinyConfig;
    uint8 pfcTotalNum;
    const Dpe_MirrorBusConfigType *mirrorBusConfig;
    uint32 *diagVcanRxFifo;
    Dpe_DiagVcanCallbackType diagVcanCallback;
} Dpe_PfcConfigType;

typedef struct
{
    uint32 base;
    uint16 dpeCanMask;
    const uint32 *canBufferBase;
    const Dpe_RouteConfigType *routeConfig;
    const Dpe_QueueConfigType *queueConfig;
    uint8 queueTotalNum;
    const Dpe_PfcConfigType *pfcConfig;
} Dpe_ConfigType;

extern const Dpe_ConfigType Dpe_Config;
extern const uint32 Dpe_CanBaseAddrConfig[16];
/** *****************************************************************************************************
 * \brief This service initializes the DPE driver.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Dpe_Init(const Dpe_ConfigType *Config)
 *
 * Service ID[hex]    : 0x00
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : mode - User mode (Non-Privileged mode).
 *
 * Parameters (in)    : Config - Dpe Configuration parameter.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : None
 *
 * Description        : Driver Module Initialization function.The Initialization function shall
 *                      initialize DPE relevant registers with the values of the structure
 *                      referenced by the parameter configPtr
 * \endverbatim
 * Traceability       :
 *******************************************************************************************************/
Std_ReturnType Dpe_Init(const Dpe_ConfigType *Config);

/** *****************************************************************************************************
 * \brief This service deinitializes the DPE driver.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Dpe_DeInit(void)
 *
 * Service ID[hex]    : 0x01
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : mode - User mode (Non-Privileged mode).
 *
 * Parameters (in)    : None.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : None
 *
 * Description        : Driver Module DeInitialization function.The Initialization function shall
 *                      deinitialize DPE relevant registers and clear dpe state.
 * \endverbatim
 * Traceability       :
 *******************************************************************************************************/
Std_ReturnType Dpe_DeInit(void);

#if (CAN_SET_PFC_API == STD_ON)
uint16 Dpe_Pfc_GetCounter(uint8 PfcIdx);
#endif/*CAN_SET_BAUDRATE_API == STD_ON*/

#if (CAN_DIAGVCAN_API == STD_ON)
void Dpe_DiagVcanIrqHandler(void);
#endif/*end of CAN_DIAGVCAN_API*/

#ifdef __cplusplus
}
#endif

#endif /* DPEH_H_ */
